package com.dronff.gvm.entity;

import com.dronff.gvm.interfaces.AllocatableMemory;
import com.dronff.gvm.interfaces.AutoGenerate;
import com.dronff.gvm.interfaces.Hashable;
import com.dronff.gvm.interfaces.Offsetable;
import com.dronff.gvm.memorymanager.Memory;
import com.dronff.gvm.memorymanager.impl.AllocatableMemoryImpl;
import com.dronff.gvm.memorymanager.impl.MiniMemoryImpl;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author: tyf
 * @date: 2022/9/10 16:58
 * @description: todo(
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MiniAddress implements Offsetable, Hashable {
    int memoryHash;
    int offset;
    int size;
    @Override
    public void setOffset(int offset) {
        this.offset = offset;
    }

    @Override
    public int getOffset() {
        return offset - AllocatableMemoryImpl.getAllocatableMemory(memoryHash).getOffset();
    }

    @Override
    public int getHash() {
        return memoryHash;
    }
    public MiniMemoryImpl getMemory() throws IllegalAccessException {
        return MiniMemoryImpl
                .getBuilder()
                .setParent(AllocatableMemoryImpl.getAllocatableMemory(memoryHash))
                .setOffset(offset)
                .setSize(size)
                .build();
    }
    @Override
    public String toString(){
        return "0x"+Integer.toHexString(hashCode())+"_"+offset;
    }
}
